﻿using eBookDirectory.Common.Dtos;
using eBookDirectory.Infrastructure.Entities;
using eBookDirectory.Infrastructure.Repository;

namespace eBookDirectory.Service.Entities
{
    /// <summary>
    /// Entity is only used by server-side
    /// </summary>
    public class Book : EntityBase
    {
        /// <summary>
        /// Verhindert die Nutzung des Entities. Nur die richtige Nutzung des Programms ist gewährleistet.
        /// </summary>
        protected Book() { }

        public string Name { get; protected set; }
        public string Description { get; protected set; }
        
        /// <summary>
        /// See how the DTO instance is used to populate the properties of a new customer instance. 
        /// Then the save method is executed against the passed repository. 
        /// Instead of using a reference to the repository implementation in our entities, 
        /// we will pass an instance of the repository to our entity methods and factories. 
        /// This approach will evolve as we cover future aspects of the application. 
        /// Another aspect to notice here is that the factory method creates persisted customer instances.
        /// </summary>
        public static Book Create(IRepositoryLocator repository, BookDto dto)
        {
            var instance = new Book
            {
                Name= dto.Name,
                Description= dto.Description
            };

            repository.Save(instance);
            return instance;
        }
    }
}
